<template>
	<view class="view">
		<view style="display: flex;flex-direction: column; width: 100%; height: 88%;">
			<view style="display: flex;justify-content: center; align-items: center;">
				<image :src="cake.cakeImage" mode="aspectFit"></image>
			</view>
			<view style="display: flex;flex-direction: row; justify-content: space-between;">
				<text>购买数:{{cake.cakeNumber}}</text>
				<uni-rate :max="5" :value="cake.cakeScore" />
			</view>
			<text style="font-size: 18px; color: #800;">${{cake.cakeMoney}}元</text>
			<text>{{cake.cakeIntroductory}}</text>

		</view>
	<view style="margin-bottom: 10px; ">
		<uni-section  type="line">
			<uni-goods-nav :buttonGroup="buttonGroup" :options="options" :fill="true"  @buttonClick="buttonClick" />
		</uni-section>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cake: {},
				options: [{
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
						color: '#fff'
					}
				],
				cakeId:null,
				userId:null
			}
		},
		methods: {
			buttonClick(e) {
				if(!uni.getStorageSync('token')){
					uni.showToast({
						title:"你还没登陆！！",
						icon:"error"
					})
					return
				}
				if(e.index==0)
				{
					uni.request({
						url: this.api.updateNumber,
						method: 'PUT',
						data: {
							cakeId:this.cakeId,
							userId:this.userId
						},
						success: res => {
							if(res.data.code==200){
								this.getOred()
							}
						},
						fail: () => {},
						complete: () => {}
					});
				}
			},
			getCake(id) {
				uni.request({
					url: this.api.findById,
					method: 'POST',
					data: {
						id: id
					},
					success: res => {
						if (res.data.code == 200) {
							this.cake = res.data.obj
							this.cake.cakeImage = "http://127.0.0.1:8989/" + this.cake.cakeImage
							uni.setNavigationBarTitle({
								title: this.cake.cakeName
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			getOred(){
				uni.request({
					url: this.api.findByUserCakeId,
					method: 'POST',
					data: {
					cakeId:this.cakeId,
					userId:this.userId
					},
					success: res => {
						if (res.data.code==200) {
							this.options[0].info=res.data.obj.number
						}
					},
					fail: () => {},
					complete: () => {}
				});
			}
		},
		onLoad(open) {
			this.cakeId=open.id
			this.getCake(this.cakeId)
			if (uni.getStorageSync("token")) {
				let user =uni.getStorageSync('token')
				this.userId=user.userId
				this.getOred()
			}
		}
	}
</script>

<style lang="scss">
	page,
	.view {
		width: 100%;
		height: 100%;
		position: relative;
	}
</style>
